//
//  DHAccountModel.m
//  DHKuaiYi
//
//  Created by Cedric Zheng on 16/11/2016.
//  Copyright © 2016 30-陈辉华. All rights reserved.
//

#import "DHAccountModel.h"

@implementation DHAccountModel

//单例对象创建方法
+ (instancetype)sharedUserAccount {
    static id instance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
        [instance loadUserAccount];
    });
    return instance;
}

/**
 *  读取用户信息
 */
- (void)loadUserAccount {
    //从沙盒中获取数据
    NSDictionary *userDict = [XHHUserDefaultsTool objetForKey:USER_ACCOUNT_KEY];
    
    //判断是否有值,如果有就给属性赋值
    if (userDict) {
        [self setValuesForKeysWithDictionary:userDict];
    }
    
    //如果过期,令牌为nil
    if (self.isExpired) {
        self.access_token = nil;
    }
}

/**
 *  保存用户信息
 *
 *  @param dict 字典参数
 */
- (void)saveUserAccount:(NSDictionary *)dict {
    //给属性赋值
    [self setValuesForKeysWithDictionary:dict];
    
    //模型转字典
    NSDictionary *userDict = [self dictionaryWithValuesForKeys:@[@"access_token", @"expires_date", @"uid", @"screen_name", @"avatar_large"]];
    
    //保存偏好设置
    [XHHUserDefaultsTool saveObject:userDict forKey:USER_ACCOUNT_KEY];
}

//注销用户
- (void)logoutCurrentUser {
    [XHHUserDefaultsTool saveObject:nil forKey:USER_ACCOUNT_KEY];
    self.access_token = nil;
}

#pragma mark - 保存图片 获取图片
//保存图片
- (void)saveImageDocuments:(UIImage *)image{

    //设置一个图片的存储路径 并且以当前登录ID 为图片名称
    NSString *pathID = [NSString stringWithFormat:@"/Documents/%@.png",self.loginID];
    NSString *imagePath = [NSHomeDirectory() stringByAppendingString:pathID];
    //把图片直接保存到指定的路径（同时应该把图片的路径imagePath存起来，下次就可以直接用来取）
    [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
}

// 从沙盒获取图片
- (UIImage *)getDocumentImage{
    // 读取沙盒路径图片
    NSString *imagePath=[NSString stringWithFormat:@"%@/Documents/%@.png",NSHomeDirectory(),self.loginID];
    // 拿到沙盒路径图片
    UIImage *imgFromPath=[[UIImage alloc]initWithContentsOfFile:imagePath];
    
    return imgFromPath;
}

#pragma mark - setter && getter 方法
- (void)setExpires_in:(NSTimeInterval)expires_in {
    _expires_in = expires_in;
    //计算过期日期
    self.expires_date = [NSDate dateWithTimeIntervalSinceNow:expires_in];
}

// account_token 是否过期
- (BOOL)isExpired {
    NSDate *currentDate = [NSDate dateWithTimeIntervalSinceNow:0];
    
    return ([self.expires_date compare:currentDate] == NSOrderedAscending);
}

- (void)setUid:(NSString *)uid {
    _uid = uid;
    if (uid) {
        self.loginID = uid;
        [XHHUserDefaultsTool saveObject:uid forKey:CURRENT_LOGIN_ACCOUNT_KEY];
    }
}


- (BOOL)isLogin{
    
    return self.access_token != nil && !self.isExpired;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}

@end
